<?php
class VmView extends DataObject
{
    public $id;
    public $modelId;
    public $allowEdit = true;
    public $allowDelete = true;
    public $allowInsert = true;
    public $viewtypeId;
    public $title;
    public $description;

    protected $viewModel;
    protected $fields = array();
    protected $actions = array();
    protected $massActions = array();
    protected $enabledFields;

    public function VmView($id = null)
    {
        parent::__construct($id);
        $model_id = $this->modelId ? $this->modelId : $id;
        $this->viewModel = ModelBase::GetInstanceByName($model_id);
        $this->ProcessRequest();
        $this->LoadFields();
        $this->LoadActions();
    }

    protected function ProcessRequest()
    {}

    protected function LoadFields()
    {
        foreach ($this->viewModel->GetFields() as $f)
        {
            $field = clone $f;
            $field->LoadView($this->id);
            $this->fields[$field->name] = $field;
        }
        uasort($this->fields, array($this, 'SortFields'));
    }

    public function SortFields($f1, $f2)
    {
        $s1 = $f1->GetSortOrder();
        $s2 = $f2->GetSortOrder();
        if ($s1 == $s2) return 0;
        return $s1 < $s2 ? -1 : 1;
    }

    protected function LoadActions()
    {
        if ($this->modelId)
        {
            $this->actions = VmActionModel::GetInstance()->GetCodeByViewId($this->id);
            $this->massActions = VmActionModel::GetInstance()->GetCodeByViewId($this->id, 1);
        }
    }

    public function GetFields()
    {
        return $this->fields;
    }

    public function GetActions()
    {
        return $this->actions;
    }

    public function GetMassActions()
    {
        return $this->massActions;
    }

    public function GetField($name)
    {
        return $this->fields[$name];
    }

    public function GetViewModel()
    {
        return $this->viewModel;
    }

    public function GetEnabledFields()
    {
        if (is_null($this->enabledFields))
        {
            $this->enabledFields = array();
            foreach ($this->fields as $field)
            {
                if ($field->enabled) $this->enabledFields[$field->name] = $field;
            }
        }
        return $this->enabledFields;
    }

    public function GetModelName()
    {
        return $this->viewModel->GetLowercaseName();
    }

    public function GetModelPrimaryKey()
    {
        return $this->viewModel->GetPrimaryKey();
    }

    public function Save()
    {
        $model_name = $this->viewModel->GetLowercaseName();
        $items = RequestHandler::Get($model_name);
        $id = null;
        if (is_array($items))
        {
            foreach($items as $id => $item)
            {
                $pk = $this->viewModel->GetPrimaryKey();
                if (isset($item[$pk]))
                    $item[ModelBase::FIELD_NEW_ID] = $item[$pk];
                $item[$pk] = $this->IsUpdate($id) ? $id : null;
                $id = $this->viewModel->Save($item);
            }
        }
        return $id;
    }

    protected function IsUpdate($id)
    {
        return substr($id, 0, 3) != 'new';
    }

    public function Delete()
    {
        $ids = array_keys(RequestHandler::Get('selected'));
        foreach($ids as $id)
        {
            $this->Del($id);
        }
    }

    public function Del($id)
    {
        try
        {
            $this->viewModel->Del($id);
            MessageBase::Confirm("record '$id' has been deleted");
        }
        catch (Exception $ex)
        {
            MessageBase::Warning($ex->getMessage());
        }
    }

    public function GetFieldGroups()
    {
        $groups = array();
        $fields = $this->GetEnabledFields();
        foreach ($fields as $field)
        {
            $groups[$field->group][$field->name] = $field;
        }
        return $groups;
    }
}
?>
